package com.java.star.mall.front.controller;

import com.github.xiaoymin.knife4j.annotations.ApiOperationSupport;
import com.java.star.mall.common.response.JsonResult;
import com.java.star.mall.front.pojo.param.UserUpdateParam;
import com.java.star.mall.front.pojo.param.WxLoginParam;
import com.java.star.mall.front.pojo.vo.WxLoginVO;
import com.java.star.mall.front.service.WxUserService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
import org.springframework.web.multipart.MultipartFile;

import javax.validation.Valid;

/**
 * 微信用户Controller
 */
@Api(value = "wx_user", tags = "微信用户")
@RestController
@RequestMapping("/wx/user")
@Validated
public class UserController {

    @Autowired
    private WxUserService wxUserService;

    @ApiOperation(value = "微信用户登录")
    @ApiOperationSupport(order = 100)
    @PostMapping("/login")
    public JsonResult<WxLoginVO> login(@Valid @RequestBody WxLoginParam wxLoginParam) {
        WxLoginVO wxLoginVO = wxUserService.login(wxLoginParam);
        return JsonResult.ok(wxLoginVO);
    }

    @ApiOperation(value = "微信用户头像上传")
    @ApiOperationSupport(order = 200)
    @PostMapping("/upload")
    public JsonResult<String> upload(MultipartFile picFile) {
        String fileUrl = wxUserService.upload(picFile);
        return JsonResult.ok(fileUrl);
    }

    @ApiOperation(value = "微信用户信息修改")
    @ApiOperationSupport(order = 300)
    @PostMapping("/update")
    public JsonResult<Void> info(@Valid @RequestBody UserUpdateParam userUpdateParam) {
        wxUserService.update(userUpdateParam);
        return JsonResult.ok();
    }
}
